package edu.whut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whut.dao.PoemDao;
import edu.whut.entity.Poem;
import edu.whut.service.PoemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class PoemServiceImpl implements PoemService {

    @Resource
    PoemDao poemDao;

    @Override
    public Boolean addPoem(Poem poem) {
        return poemDao.insert(poem)>0;
    }

    @Override
    public Boolean deletePoem(Long poemId) {
        return poemDao.deleteById(poemId)>0;
    }

    @Override
    public Boolean updatePoem(Poem poem) {
        return poemDao.updateById(poem)>0;
    }

    @Override
    public Poem getPoem(Long poemId) {
        return poemDao.selectById(poemId);
    }

    @Override
    public List<Poem> getPoemByType(String type,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("type",type);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByWriter(String writer,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("author",writer);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByDynasty(String dynasty,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("dynasty",dynasty);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getAllPoem(Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByTitle(String title,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("title",title);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> getPoemByPassage(String passage,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("passage",passage);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public List<Poem> fuzzySearch(String field,Integer cnt) {
        Page<Poem> page=new Page<Poem>(cnt,10);
        QueryWrapper<Poem> wrapper=new QueryWrapper<Poem>();
        wrapper.like("title",field)
                .like("writer",field)
                .like("dynasty",field)
                .like("type",field)
                .like("passage",field);
        return poemDao.selectPage(page,wrapper).getRecords();
    }

    @Override
    public Poem getOneByTitle(String title) {
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("title",title);
        if(poemDao.selectByMap(map)!=null){
            return poemDao.selectByMap(map).get(0);
        }
        return null;
    }
}
